package com.xizimall.search.service.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xizimall.search.mapper.ItemMapper;
import com.xizimall.search.pojo.Item;
import com.xizimall.search.service.ItemService;
import com.xizimall.common.pojo.XizimallResult;
import com.xizimall.common.utils.ExceptionUtil;
import com.xizimall.common.utils.JsonUtils;
/**
 * 商品服务
 * <p>Title: ItemServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月24日下午7:12:42
 * @version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {
    
	@Autowired
	private ItemMapper itemMapper;
	
	@Autowired
	private SolrServer solrServer;
	
	@Override
	public XizimallResult getItemList() {
		try {
			List<Item> list=itemMapper.getItemList();
			//把商品写入数据库
			for(Item item:list){
				SolrInputDocument document=new SolrInputDocument();
				document.setField("id", item.getId());
				document.setField("item_title", item.getTitle());
				document.setField("item_sell_point", item.getSell_point());
				document.setField("item_price", item.getPrice());
				document.setField("item_image", item.getImage());
				document.setField("item_category_name", item.getCategory_name());
				document.setField("item_desc", item.getItem_des());
				//写入索引库
				solrServer.add(document);
			   
			}
			solrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
			XizimallResult.build(500, ExceptionUtil.getStackTrace(e));
			
		}
		return XizimallResult.ok();
	}

}
